package com.iqiyi.dbagent.base.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.iqiyi.dbagent.base.config.MySqlConfig;
import com.iqiyi.dbagent.base.config.i.Configuration;
import com.iqiyi.dbagent.base.i.IExcutorCallable;
import com.iqiyi.dbagent.base.utils.data.DataSourceUtil;

/**
 * @Title: ISqlExcutorCallableImpl.java
 * @Package com.iqiyi.dbagent.base.impl
 * @Description: 带返回值的多线程sql执行callable
 * @author wangnianming
 * @date 2015年11月17日 下午5:08:41
 * @version V1.0
 */
public class ISqlExcutorCallableImpl implements IExcutorCallable {

    private List<String> bufferList = null;

    private final static Logger logger = LoggerFactory.getLogger(ISqlExcutorCallableImpl.class);
    
    private Configuration config = null;
    
    public ISqlExcutorCallableImpl() {
        // TODO Auto-generated constructor stub
    }

    public List<String> getBufferList() {
        return bufferList;
    }

    public void setBufferList(List<String> bufferList) {
        this.bufferList = bufferList;
    }

    public Configuration getConfig() {
        return config;
    }

    public void setConfig(Configuration config) {
        this.config = config;
    }

    public ISqlExcutorCallableImpl(List<String> bufferList,Configuration config) {
        super();
        this.bufferList = bufferList;
        this.config = config;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.concurrent.Callable#call()
     */
    public Object call(){
        Map<Connection,Integer> resMap = new ConcurrentHashMap<Connection,Integer>();
        Connection conn = null;
        int rs = 0;
        if (null != bufferList) {
            Statement stmt = null;
            long step = 0;
            int[] ssr = null;
            try {
                conn = DataSourceUtil.getSyncConn(DataSourceUtil.DRUID_MYSQL_SOURCE,MySqlConfig.getInstance());
                stmt = conn.createStatement();
                for(String sql:bufferList){
                    //logger.info(sql + "************");
                    stmt.addBatch(sql);
                    step++;
                    if(step == 5000){
                        logger.info("execCount:{}",5000);
                        ssr = stmt.executeBatch();
                        rs += ssr.length;
                        stmt.clearBatch();
                        step = 0;
                    }
                }
                if (step > 0){
                    logger.info("execCount:{}",step);
                    ssr = stmt.executeBatch();
                    rs += ssr.length;
                    stmt.clearBatch();
                }
            } catch (SQLException e) {
                try {
                    conn.rollback();
                } catch (SQLException es) {
                    es.printStackTrace();
                }
                logger.error("Error info => exception-sql:{}",e.getMessage());
            } catch (Exception e) {
                logger.error("Error info => exception-e:{}",e.getMessage());
            }finally{
                try {
                    if(null != stmt){
                        stmt.close();
                    }
                } catch (SQLException e) {
                    logger.error("Error info => exception-close:{}",e.getMessage());
                }
                //conn.setAutoCommit(true);
                //conn.close();
            }
            resMap.put(conn, rs);
        }
        return resMap;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.iqiyi.dbagent.base.i.IExcutorCallable#Callable()
     */
    public List<Object> Callable() {
        // TODO Auto-generated method stub
        return null;
    }

}
